/**
 * @file   1025.cpp
 * @author Shuang Hu <hsmath@ubuntu>
 * @date   Sat Jul 24 23:52:49 2021
 * 
 * @brief  PAT advanced 1025
 * 
 * 
 */
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
class student{
public:
    int score;
    string ID;
    int grouprank;
    int allrank;
    int groupID;
};
bool cmp(student& s1,student& s2){
    if(s1.score>s2.score){
	return true;
    }else if(s1.score==s2.score){
	if(s1.ID<s2.ID){
	    return true;
	}
    }
    return false;
}
class exam{
private:
    vector<student> candidates;
    int groupnum;
    int stunum;
public:
    exam();
    void solve();
    void print();
};
exam::exam(){
    cin>>groupnum;
    stunum=0;
    for(int i=1;i<=groupnum;i++){
	vector<student> mygroup;
	int stus=0;
	cin>>stus;
	stunum+=stus;
	for(int j=0;j<stus;j++){
	    student stu;
	    cin>>stu.ID>>stu.score;
	    stu.groupID=i;
	    mygroup.push_back(stu);
	}
	sort(mygroup.begin(),mygroup.end(),cmp);
	for(int j=0;j<stus;j++){
	    mygroup[j].grouprank=j+1;
	    if(j!=0){
		if(mygroup[j].score==mygroup[j-1].score){
		    mygroup[j].grouprank=mygroup[j-1].grouprank;
		}
	    }
	    candidates.push_back(mygroup[j]);
	}
    }
}
void exam::solve(){
    sort(candidates.begin(),candidates.end(),cmp);
    for(int i=0;i<candidates.size();i++){
	candidates[i].allrank=i+1;
	if(i!=0){
	    if(candidates[i].score==candidates[i-1].score){
		candidates[i].allrank=candidates[i-1].allrank;
	    }
	}
    }
}
void exam::print(){
    cout<<stunum<<endl;
    for(int i=0;i<candidates.size();i++){
	cout<<candidates[i].ID<<" "<<candidates[i].allrank<<" "<<candidates[i].groupID<<" "<<candidates[i].grouprank<<endl;
    }
}
int main(){
    exam Ex;
    Ex.solve();
    Ex.print();
}
